package lab03.test07;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;


public class Test07 {
    public static void main(String[] args) throws IOException {
        String path = "F:\\book.zip";  //表示压缩包的路径

        File outFile = null;  //表示输出的文件对象
        ZipFile zipFile = new ZipFile(path);

        InputStream input = null;//定义输入流
        OutputStream out = null; //定义输出流
        ZipInputStream zipInputStream = null;

        try {
            zipInputStream = new ZipInputStream(new FileInputStream(path));
            ZipEntry entry;
            while ((entry = zipInputStream.getNextEntry()) != null) {
                System.out.println("解压文件" + entry.getName());  //得到压缩包中文件的名字

                outFile = new File("F:\\book" + File.separator + entry.getName());
                outFile.createNewFile();        //在book文件下建立压缩包的每一个文件

                input = zipFile.getInputStream(entry);  //读取zip中的每一个entry的内容
                out = new FileOutputStream(outFile);  //实例化输出流

                int ch = 0;
                while ((ch = input.read()) != -1) {
                    out.write(ch);        //向out的路径中的文件写入
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                zipInputStream.close();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
